package com.company3;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HashMapCount {
    public static void main(String[] args) {
        //定义字符串数组，以，分隔
        String[] str ="123,456,789,123,456".split(",");
        //定义hashmap
        Map<String,Integer> strMap = new HashMap<>();
        //遍历字符串数组
        for (String s : str) {
            //如果没有则添加，如果有计数加一
            if (!strMap.containsKey(s)) {
                strMap.put(s, 1);
            } else {
                strMap.put(s, strMap.get(s) + 1);
            }
        }
        // 打印结果
        Set<String> keys = strMap.keySet();
        for (String key : keys) {
            System.out.printf("%s出现了%d次;\n",key,strMap.get(key));
        }
    }
}
